package com.example.employee.service.impl;

import com.example.common.constant.MessageConstant;
import com.example.common.context.BaseContext;
import com.example.common.domain.Result;
import com.example.common.exception.UpdatePasswordException;
import com.example.employee.domain.dto.UpdateEmployeeDTO;
import com.example.employee.domain.dto.UpdatePasswordDTO;
import com.example.employee.domain.po.Employee;
import com.example.employee.domain.vo.EmployeeInfoVO;
import com.example.employee.mapper.EmployeeMapper;
import com.example.employee.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 修改员工信息
     *
     * @param updateEmployeeDTO
     */
    @Override
    public void updateUserInfo(UpdateEmployeeDTO updateEmployeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(updateEmployeeDTO, employee);
        // 设置id
        Long currentId = BaseContext.getCurrentId();
        employee.setId(currentId);
        employee.setUpdateTime(LocalDateTime.now());
        employeeMapper.update(employee);
    }

    @Override
    public Employee getEmployeeById(Long id) {
        return employeeMapper.getEmployeeById(id);
    }

    /**
     * 修改密码
     *
     * @param updatePasswordDTO
     */
    @Override
    public void updatePassword(UpdatePasswordDTO updatePasswordDTO) {
        String oldPwd = updatePasswordDTO.getOldPassword();
        String newPwd = updatePasswordDTO.getNewPassword();
        String rePwd = updatePasswordDTO.getRePassword();

        //原密码是否正确
        Long employeeId = BaseContext.getCurrentId();
        Employee employee = employeeMapper.getEmployeeById(employeeId);
        if(!employee.getPassword().equals(DigestUtils.md5DigestAsHex(oldPwd.getBytes()))){
            throw new UpdatePasswordException(MessageConstant.OLD_PASSWORD_ERROR);
        }
        //newPwd与rePwd是否一致
        if(!newPwd.equals(rePwd)){
            throw new UpdatePasswordException(MessageConstant.TWO_PASSWORD_NOT_CONSISTENT);
        }
        //原密码和新密码是否相同
        if(oldPwd.equals(newPwd)){
            throw new UpdatePasswordException(MessageConstant.OLD_NEW_PASSWORD_SAME);
        }
        Long currentId = BaseContext.getCurrentId();
        employeeMapper.updatePwd(DigestUtils.md5DigestAsHex(newPwd.getBytes()), currentId);
    }

    /**
     * 获取员工信息
     *
     * @return
     */
    @Override
    public EmployeeInfoVO getEmployeeInfo() {
        Long userId = BaseContext.getCurrentId();
        EmployeeInfoVO employeeInfoVO = employeeMapper.selectEmployeeInfo(userId);
        employeeInfoVO.setPhone(employeeInfoVO.getPhone().substring(0,3)+"****"+employeeInfoVO.getPhone().substring(7));
        return employeeInfoVO;
    }

}
